package com.ssky.bean.validator.core;


import com.ssky.bean.validator.core.IValidatorContext;
import com.ssky.bean.validator.core.checker.IChecker;
import com.ssky.bean.validator.core.exception.VldException;

import java.util.Map;


/**
 * @Author: ssky
 * @Date: 2021/07/20
 * @Description:
 */
public class VldConditionHanlder {
    private Map<String, IChecker> checkerMap;

    public VldConditionHanlder(Map<String, IChecker> checkerMap) {
        this.checkerMap = checkerMap;
    }

    public boolean exe(String ruleId, Object local, Object bean, IValidatorContext context) {
        if (this.checkerMap.get(ruleId) == null) {
            throw new VldException(":not found condition(checker) by ruleId:" + ruleId);
        }
        Boolean rst = context.getRuleRstCache(ruleId);
        if (rst == null) {
            return this.checkerMap.get(ruleId).exeCondition(local, bean, context);
        } else {
            return rst;
        }
    }

    public Map<String, IChecker> getCheckerMap() {
        return checkerMap;
    }

}